Page({
    data: {
      sumHeight : 0 ,
      height: 0,
      currentTab : 0, //swiper 0
      params: {
        option: -2,
        pageNum: 1,
        pageSize: 999999
      },

      loading: true,
      loading2: true,
      loading3: true,
      loading4: true,
      list: [],
      list2: [],
      list3: [],
      list4: []
    },

    getHeight() {
      var _this = this
      wx.getSystemInfo({
        success: function (res) {
          let clientHetght = res.windowHeight
          let clientWidth = res.windowWidth
          let ratio = 750 / clientWidth
          let height = clientHetght * ratio
          _this.setData({
            height: height
          })
        },
      })
    },
    onLoad () {
      // this.getlistItem()
      this.getHeight()
    },

    onShow () {
      this.getList(this.data.params)
    },

    // onLoad () {

    // },

    //打开订单详情
    orderDetail (e) {

      console.log(e)
      var arr = []

      var bookIdstr = ""
      var bookArr = e.currentTarget.dataset.bookinfos

      var orderNo = e.currentTarget.dataset.orderno

      var lefttime = e.currentTarget.dataset.lefttime


      for (var i = 0; i < this.data.list.length; i++) {

        if (orderNo == this.data.list[i].id) {
          console.log(this.data.list[i].bookInfos)


          if (this.data.list[i].bookInfos.length == 1) {
            bookIdstr = this.data.list[i].bookInfos[0].id + '_' + this.data.list[i].bookInfos[0].bookQuality
          } else {
            for (var j = 0; j < this.data.list[i].bookInfos.length; j++) {
              bookIdstr += this.data.list[i].bookInfos[j].id + '_' + this.data.list[i].bookInfos[j].bookQuality + ","
            }
          }

          for (var j = 0; j < this.data.list[i].bookInfos.length; j++) {
            arr.push({
              bookId: this.data.list[i].bookInfos[j].id,
              coverPic: this.data.list[i].bookInfos[j].coverPic,
              name: this.data.list[i].bookInfos[j].name,
              price: this.data.list[i].bookInfos[j].price
            })
          }
        }
      }

      var order = e.currentTarget.dataset.orderno
      wx.navigateTo({
        url: '/package/my/pages/orderDetail/orderDetail?orderno=' + order + '&bookArr=' + encodeURIComponent(JSON.stringify(arr)) + '&orderId=' + e.currentTarget.dataset.orderid + "&lefttime=" + lefttime + "&bookIdstr=" + bookIdstr + '&orderIds=' + orderNo,
      })
    },
    //获取列表标签高度
    getlistItem () {
      var _this = this
      const query = wx.createSelectorQuery()
      query.select('.content_list').boundingClientRect()
      query.exec(function (res) {
        console.log(res[0].height)
        let sumHeight  = res[0].height
        _this.setData({
          sumHeight: sumHeight*3 + (3*30)
        })
        console.log(_this.data.sumHeight)
        // _this.sumHeight
      })
    },

  //点击头部切换
    tabClick (e) {
      console.log(e.currentTarget.dataset.current)
      var curr = e.currentTarget.dataset.current
      var options = "params.option"
      this.setData({
        currentTab: curr,
        [options]: curr == 0 ? -2 : curr == 1 ? -1 : curr == 2 ? '0,1' : curr == 3 ? 2 : ''
      })

      if (this.data.params.option == -2) {
        if (this.data.loading == false) {
          return false
        }else{
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == -1) {
        if (this.data.loading2 == false) {
          return false
        }else{
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == '0,1') {
        if (this.data.loading3 == false) {
          return false
        }else{
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == 2) {
        if (this.data.loading4 == false) {
          return false
        }else{
          this.getList(this.data.params)
        }
      }

    },
  //swiper切换
    switchTab (event) {
      console.log(event.detail.current)
      var curr = event.detail.current
      var options = "params.option"
      this.setData({
        currentTab: curr,
        [options]: curr == 0 ? -2 : curr == 1 ? -1 : curr == 2 ? '0,1' : curr == 3 ? 2 : ''
      })

      if (this.data.params.option == -2) {
        if (this.data.loading == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == -1) {
        if (this.data.loading2 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == '0,1') {
        if (this.data.loading3 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == 2) {
        if (this.data.loading4 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }
    },


    //获取订单列表

    getList (params) {

      var _this = this
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/purchase/list/auth',
        data: params,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {
            if (params.option == -2) {
              var arr = res.data.data.resultList
              _this.setData({
                list: arr,
                loading: false
              })
            }

            if (params.option == -1) {
              var arr = res.data.data.resultList
              _this.setData({
                list2: arr,
                loading2: false
              })
            }

            if (params.option == '0,1') {
              var arr = res.data.data.resultList
              _this.setData({
                list3: arr,
                loading3: false
              })
            }

            if (params.option == 2) {
              var arr = res.data.data.resultList
              _this.setData({
                list4: arr,
                loading4: false
              })
            }



          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    },

  //确认收获
  sureOrder (e) {
    console.log(111111)
    console.log(e)

    var orderId = e.currentTarget.dataset.orderid

  var orderId2 = e.currentTarget.dataset.orderno
    this.sureorderApi(orderId2)
  },

  sureorderApi (orderId) {
    var _this = this
    var params = {
      orderId: orderId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/purchase/receipt/confirm/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '确认收货成功',
            icon: 'none'
          })
          if (_this.data.params.option == -2) {
            _this.setData({
              loading2: true,
              loading3: true,
              loading4: true
            })
            _this.getList(_this.data.params)
            return
          }

          if (_this.data.params.option == -1) {
            _this.setData({
              loading: true,
              loading3: true,
              loading4: true
            })
            _this.getList(_this.data.params)

            return
          }
          if (_this.data.params.option == '0,1') {
            _this.setData({
              loading: true,
              loading2: true,
              loading4: true
            })
            _this.getList(_this.data.params)

            return
          }
          if (_this.data.params.option == 2) {
            _this.setData({
              loading: true,
              loading2: true,
              loading3: true
            })
            _this.getList(_this.data.params)

            return
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //去支付

  goBuy(e) {
  
    var bookIdstr = ""

    var arr = []
    var bookArr = e.currentTarget.dataset.bookinfos

    var orderNo = e.currentTarget.dataset.orderno

    for (var i = 0; i < this.data.list.length; i++) {

      if (orderNo == this.data.list[i].id) {
        console.log(this.data.list[i].bookInfos)


        if (this.data.list[i].bookInfos.length == 1) {
          bookIdstr = this.data.list[i].bookInfos[0].id + '_' + this.data.list[i].bookInfos[0].bookQuality
        } else {
          for (var j = 0; j < this.data.list[i].bookInfos.length; j++) {
            bookIdstr += this.data.list[i].bookInfos[j].id + '_' + this.data.list[i].bookInfos[j].bookQuality + ","
          }
        }

        for (var j = 0; j < this.data.list[i].bookInfos.length; j++) {
          arr.push({
            bookId: this.data.list[i].bookInfos[j].id,
            coverPic: this.data.list[i].bookInfos[j].coverPic,
            name: this.data.list[i].bookInfos[j].name,
            price: this.data.list[i].bookInfos[j].price
          })
        }
      }
    }


    console.log(bookIdstr)

    wx.navigateTo({
      url: '/package/index/pages/buyBook/buyBook?bookArr=' + encodeURIComponent(JSON.stringify(arr)) + '&orderId=' + e.currentTarget.dataset.orderid + '&bookIdstr=' + bookIdstr + '&orderIds=' + orderNo
    })
 
  },

  returnOrder () {
    return false
  },

  // 上拉加载加载更多

})
